package com.gusha.controller;

import java.math.BigInteger;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import com.gusha.config.SysOperateLog;
import com.gusha.dao.SysShoppingDao;
import com.gusha.model.SysShopping;
import com.gusha.service.SysShoppingService;
import com.gusha.utils.Constants;
import com.gusha.utils.FileUtils;
import com.gusha.utils.JsonResult;
import com.gusha.utils.WebValueUtil;

@Controller
@RequestMapping("/gx/sysShopping")
public class SysShoppingController {
	
	@Autowired
	private SysShoppingDao sysShoppingDao;
	@Autowired
	private SysShoppingService sysShoppingService;
	
	
	@RequestMapping("list")
    @SysOperateLog(operate = "查看商城列表")
	public String list(Model model, HttpServletRequest request) {
        //请求参数
        String pageIndex = WebValueUtil.getWebValue(request, "pageIndex");
        String commodityName = WebValueUtil.getWebValue(request, "commodityName");
        String commodityType = WebValueUtil.getWebValue(request, "commodityType");
        int pageNo = 0;
        if (StringUtils.isNotBlank(pageIndex)) {
            pageNo = Integer.parseInt(pageIndex);
        }
        
        SysShopping sysShopping=new SysShopping();
        if(StringUtils.isNotBlank(commodityName)) {
        sysShopping.setCommodityName(commodityName);
        }
        if(StringUtils.isNotBlank(commodityType)) {
            sysShopping.setCommodityType(new BigInteger(commodityType));
            }
        Sort sort = new Sort(Sort.Direction.DESC, "addTime");
        Pageable pageable = PageRequest.of(pageNo, Constants.DEFAULT_PAGESIZE, sort);
        Page<SysShopping> page = sysShoppingService.findAll(pageable, sysShopping);
        model.addAttribute("page", page);

        return "/admin/sysShopping/list";
		
	}
	
	@RequestMapping("toAdd")
    @SysOperateLog(operate = "进入商城添加页面")
	public String toAdd() {
		return "/admin/sysShopping/form";
		
	}
	
    @RequestMapping("save")
    @ResponseBody
    @SysOperateLog(operate = "保存产品信息")
    public JsonResult save(SysShopping sysShopping,HttpServletRequest request) {
        JsonResult result = new JsonResult(false,"保存失败");
        String commodityImgOld = WebValueUtil.getWebValue(request, "commodityImgOld");
        if(!commodityImgOld.equals(sysShopping.getCommodityImg())) {
        	 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
             MultipartFile file = multipartRequest.getFile("file");
              // 要上传的目标文件存放路径
             String newFileName = FileUtils.getFileName(file.getOriginalFilename());
            // String newFileName = file.getOriginalFilename();
           String imagePath = Constants.getFilePath("commodity")+newFileName;
             String localPath = Constants.IMAGE_LOCAL_PATH+Constants.getFilePath("commodity");
            // String localPath = Constants.IMAGE_LOCAL_PATH;
             FileUtils.upload(file, localPath, newFileName);
             sysShopping.setCommodityImg(imagePath);
        }
        sysShoppingDao.saveAndFlush(sysShopping);
        result.setFlag(true);
        result.setMsg("保存成功");
        return result;
    }

	
    // 进入编辑页面
    @RequestMapping("toEdit")
    @SysOperateLog(operate = "进入产品编辑页面")
    public String toEdit(Model model, HttpServletRequest request) {

        //请求参数
        String id = WebValueUtil.getWebValue(request, "id");

        if (StringUtils.isNotBlank(id)) {
            if(sysShoppingDao.existsById(new BigInteger(id))) {
            	SysShopping sysShopping = sysShoppingDao.getOne(new BigInteger(id));
                model.addAttribute("obj", sysShopping);
            }
        }        
        return "/admin/sysShopping/form";
    }
    
    @RequestMapping("delete")
    @ResponseBody
    @SysOperateLog(operate = "删除资讯信息")
    public JsonResult delete(BigInteger id) {
        JsonResult result = new JsonResult(false,"删除失败");
        if (sysShoppingDao.existsById(id)) {
        	SysShopping sysShopping = sysShoppingDao.getOne(id);
        	sysShopping.setIsDelete(new BigInteger("1"));
        	sysShoppingDao.saveAndFlush(sysShopping);
            result.setFlag(true);
            result.setMsg("删除成功");
        }else{
            result.setFlag(false);
            result.setMsg("该对象信息不存在");
            return result;
        }
        return result;
    }


    // 恢复
    @RequestMapping("recovery")
    @ResponseBody
    @SysOperateLog(operate = "恢复资讯信息")
    public JsonResult recovery(BigInteger id) {
    	 JsonResult result = new JsonResult(false,"删除失败");
         if (sysShoppingDao.existsById(id)) {
         	SysShopping sysShopping = sysShoppingDao.getOne(id);
         	sysShopping.setIsDelete(new BigInteger("0"));
         	sysShoppingDao.saveAndFlush(sysShopping);
             result.setFlag(true);
             result.setMsg("删除成功");
         }else{
             result.setFlag(false);
             result.setMsg("该对象信息不存在");
             return result;
         }
         return result;
    }
}
